﻿<div class="dialog" id="publisherDialog">
    <form action="">
        <input type="hidden" name="PublisherId" />
        
        <label for="publisherNameTextbox">Publisher Name</label>
        <input type="text" id="publisherNameTextbox" name="PublisherName" /><br /><br />
        
        <label for="cityTextbox">City</label>
        <input type="text" id="cityTextbox" name="City" /><br />
    </form>
</div>

<script type="text/javascript">
    var PublisherDialog = new Object();

    PublisherDialog.open = function (publisher, callback) {
        PublisherDialog.publisher = new Object();
        PublisherDialog.publisher.City = publisher.City;
        PublisherDialog.publisher.PublisherId = publisher.PublisherId;
        PublisherDialog.publisher.PublisherName = publisher.PublisherName;

        PublisherDialog.onClose = callback;

        $('#publisherDialog form')
            .databind(PublisherDialog.publisher)
            .jqValidate("clear");

        $('#publisherDialog')
            .dialog("option", "title", PublisherDialog.publisher.PublisherId ? "Edit Publisher" : "Create Publisher")
            .dialog('open');
        PublisherDialog.state = "open";
    }

    PublisherDialog.state = "unopened";

    function SavePublisher() {
        $('#publisherDialog form').jqValidate('@Url.Action("PublisherSave")', PublisherDialog.publisher, function (data) {
            if (data.isValid) {
                PublisherDialog.state = "save";
                $('#publisherDialog').dialog('close');
            }
        });
    }

    $(function () {
        $("#publisherDialog").dialog({
            autoOpen: false,
            buttons: [
                { text: "Save", click: SavePublisher },
                { text: "Cancel", click: function () { $('#publisherDialog').dialog('close'); } }
            ],
            close: function () {
                var wasSaved = (PublisherDialog.state === "save");
                PublisherDialog.state = wasSaved ? "close" : "cancel";
                if (PublisherDialog.onClose)
                    PublisherDialog.onClose(wasSaved);
            },
            modal: true,
            resizable: false
        });
        // Unfortunately, the current version of the jQuery dialog does not support specifying button icons
        // within the dialog setttings. So, we do it the hard way:
        var buttonPane = $('#publisherDialog').parent().find('.ui-dialog-buttonpane');
        $(buttonPane).find('button:contains("Save")').button({ icons: { primary: "ui-icon-disk"} });
        $(buttonPane).find('button:contains("Cancel")').button({ icons: { primary: "ui-icon-cancel"} });
    });
</script>